import java.util.concurrent.atomic.AtomicInteger;

class CustomTask implements Runnable {
    AtomicInteger  num;

    public CustomTask(AtomicInteger num) {
        this.num = num;
    }
    @Override
    public void run() {
        try {
            if (Thread.currentThread().isInterrupted()) {
                throw new InterruptedException("Task interrupted");
            }

            if (num.get() < 0) {
                throw new IllegalArgumentException("Invalid argument: " + num);
            }

            System.out.println("Task is running with num = " + num);

        } catch (InterruptedException e) {
            System.out.println("InterruptedException caught in task: " + e.getMessage());
        } catch (IllegalArgumentException e) {
            System.out.println("IllegalArgumentException caught in task: " + e.getMessage());
        } finally {
            System.out.println("Task finally block executed.");
        }
    }
}

public class AdvancedException {
    public static void main(String[] args) {
        //use atomic integer for thread safe
        var thread1 = new Thread(new CustomTask(new AtomicInteger(10)));
        var thread2 = new Thread(new CustomTask(new AtomicInteger(-2)));

        thread1.start();
        thread2.start();

        try {
            Thread.sleep(1000);

            // Interrupting thread1 to demonstrate InterruptedException handling
            thread1.interrupt();

            thread1.join();
            thread2.join();
        } catch (InterruptedException e) {
            System.out.println("Main thread interrupted: " + e.getMessage());
        } finally {
            System.out.println("Main thread finally block executed.");
        }
    }
}
